<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/stores/data'
const store = useStore();
const router = useRouter()

const messages = ref([
  { type: 'ai', content: '您好！我是DeepSeek AI助手，请问有什么可以帮您？' }
])
const userInput = ref('')
const loading = ref(false)
const chatContent = ref(null)

const sendMessage = async () => {
  if (!userInput.value.trim() || loading.value) return
  messages.value.push({
    type: 'user',
    content: userInput.value
  })

   // 显示AI正在输入
  loading.value = true
  messages.value.push({
    type: 'ai',
    content: ''
  })

   try {
    // 模拟AI回复
    //  store.deepseek(userInput.value, '测试用户').then(res => {
    //     console.log(res)
    //     const aiResponse = res.response
    //     messages.value[messages.value.length - 1].content = aiResponse
    //   })
    console.log(store)
    const aiResponse = await store.deepseek(userInput.value, '测试用户')


    messages.value[messages.value.length - 1].content = aiResponse.response

    console.log(aiResponse)
    
    // 替换为实际的API调用
    // const response = await fetchDeepSeekAPI(userMessage) 
  } catch (error) {
    console.log(error)
    messages.value[messages.value.length - 1].content = '抱歉，暂时无法获取回复，请稍后再试。'
  } finally {
    loading.value = false
    scrollToBottom()
  }

 

  // 添加用户消息
  
  
  const userMessage = userInput.value
  userInput.value = ''
  
  // 滚动到底部
  scrollToBottom()
  
 
  
 
}

const scrollToBottom = () => {
  nextTick(() => {
    if (chatContent.value) {
      chatContent.value.scrollTop = chatContent.value.scrollHeight
    }
  })
}
</script>

<template>
  <div class="pages">
    <!-- 头部导航 -->
		<div class="header animate__fadeInDown">
			<button class="back-button fff" @click="router.back()">
				<span class="back-icon">←</span>
			</button>
			<span class="header-title fff">AI测试</span>
		</div>
    <div class="deepseek-chat">
      <!-- 聊天内容区域 -->
      <div class="chat-content" ref="chatContent">
        <div v-for="(message, index) in messages" :key="index" 
            :class="['message', message.type]">
          <div class="message-content">
            <div v-if="message.type === 'ai'" class="ai-avatar">
              <img src="@/assets/images/config/deepseek/deepseek.png" alt="AI Avatar">
            </div>
            <div class="text" :class="message.type">
              {{ message.content }}
            </div>
          </div>
        </div>
        <div v-if="loading" class="typing-indicator">
          <span></span>
          <span></span>
          <span></span>
        </div>
      </div>

      <!-- 输入区域 -->
      <div class="chat-input">
        <textarea v-model="userInput" placeholder="输入您的问题..." 
                  @keyup.enter="sendMessage"></textarea>
        <!-- <button @click="sendMessage" :disabled="loading">
          <img src="@/assets/images/config/deepseek/send.png" alt="Send">
        </button> -->
        <div class="sendMessage" @click="sendMessage" :disabled="loading">
          <img src="@/assets/images/config/deepseek/send.png" alt="Send">
        </div>
      </div>
    </div>
  </div>
</template>



<style lang="scss" scoped>
.deepseek-chat {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #fff;
  border-radius: 20px 20px 0 0; // 35rpx / 1.7441 = 20px
}

.chat-header {
  background: linear-gradient(90deg, #006CFF 0%, #009FFF 100%);
  color: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  
  .logo {
    width: 32px;
    height: 32px;
    margin-right: 12px;
  }
  
  h2 {
    margin: 0;
    font-size: 18px;
  }
}

.chat-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  
  .message {
    margin-bottom: 16px;
    
    .message-content {
      display: flex;
      align-items: flex-start;
      
      .ai-avatar {
        margin-right: 10px;
        
        img {
          width: 32px;
          height: 32px;
          border-radius: 50%;
        }
      }
    }
    
    .text {
      max-width: 80%;
      padding: 12px 16px;
      border-radius: 18px;
      line-height: 1.5;
      word-wrap: break-word;
      font-size: 14px;
      &.user {
        background: linear-gradient(90deg, #006CFF 0%, #009FFF 100%);
        color: white;
        margin-left: auto;
        border-radius: 18px 0 18px 18px;
      }
      
      &.ai {
        background-color: white;
        color: #333;
        border-radius: 0 18px 18px 18px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }
    }
  }
}

.typing-indicator {
  display: flex;
  padding: 10px 16px;
  
  span {
    width: 8px;
    height: 8px;
    background-color: #999;
    border-radius: 50%;
    margin: 0 4px;
    animation: typing 1s infinite ease-in-out;
    
    &:nth-child(1) { animation-delay: 0.2s; }
    &:nth-child(2) { animation-delay: 0.3s; }
    &:nth-child(3) { animation-delay: 0.4s; }
  }
}

.chat-input {
  display: flex;
  padding: 15px;
  background-color: white;
  border-top: 1px solid #eee;
  align-items: center;
  
  textarea {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 10px 15px;
    resize: none;
    height: 40px;
    max-height: 120px;
    outline: none;
  }
  
  .sendMessage {
    width: 35px;
    height: 35px;
    border: none;
    background-color: #007AFF;
    border-radius: 50%;
    margin-left: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    
    &:disabled {
      opacity: 0.6;
    }
    
    img {
      width: 20px;
      height: 20px;
    }
  }
}

@keyframes typing {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}
</style>